package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.model.MarketSystem;
import com.cskaoyan.model.MarketSystemExample;

import com.cskaoyan.service.SystemService;
import com.cskaoyan.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;
import java.util.stream.Collectors;

public class SystemServiceImpl implements SystemService {
    @Override
    public void updateMall(Map map) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        Set<String> keys = map.keySet();
        criteria.andKeyNameIn(keys.stream().toList());
        List<MarketSystem> marketSystems = systemMapper.selectByExample(example);
        for (MarketSystem system : marketSystems) {
            system.setKeyValue((String) map.get(system.getKeyName()));
            system.setUpdateTime(new Date());
            systemMapper.updateByPrimaryKeySelective(system);
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Map<String, String> queryMall() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        example.createCriteria().andKeyNameLike("%market_mall_%");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(example);
        Map<String, String> dataMap = marketSystems.stream()
                .collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));
        sqlSession.commit();
        sqlSession.close();
        return dataMap;
    }

    @Override
    public void updateWx(Map<String, Object> map) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        Set<String> keys = map.keySet();
        criteria.andKeyNameIn(keys.stream().toList());
        List<MarketSystem> marketSystems = systemMapper.selectByExample(example);
        for (MarketSystem marketSystem : marketSystems) {
            Object value = map.get(marketSystem.getKeyName());
            marketSystem.setKeyValue(String.valueOf(value));
            marketSystem.setUpdateTime(new Date());
            systemMapper.updateByPrimaryKeySelective(marketSystem);
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Map<String, String> queryWx() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        example.createCriteria().andKeyNameLike("%market_wx_%");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(example);
        Map<String, String> map = marketSystems.stream().collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));
        sqlSession.commit();
        sqlSession.close();
        return map;
    }

    @Override
    public HashMap<String, String> getExpressConfig() {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketSystemMapper mapper = sqlsession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        String[] strs = {"market_express_freight_min", "market_express_freight_value"};
        criteria.andKeyNameIn(Arrays.asList(strs));

        List<MarketSystem> marketSystemList = mapper.selectByExample(marketSystemExample);
        HashMap<String, String> hashMap = new HashMap<>();
        for (MarketSystem marketSystem : marketSystemList) {
            if (marketSystem != null) {
                String keyName = marketSystem.getKeyName();
                String keyValue = marketSystem.getKeyValue();
                hashMap.put(keyName, keyValue);
            }
        }

        sqlsession.close();
        return hashMap;
    }

    @Override
    public void postExpressConfig(HashMap<String, String> hashMap) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketSystemMapper mapper = sqlsession.getMapper(MarketSystemMapper.class);

        String keyName1 = "market_express_freight_min";
        String keyValue1 = hashMap.get(keyName1);
        String keyName2 = "market_express_freight_value";
        String keyValue2 = hashMap.get(keyName2);

        Date date = new Date();
        mapper.updateExpressConfig(keyName1, keyValue1, date);
        mapper.updateExpressConfig(keyName2, keyValue2, date);

        sqlsession.commit();
        sqlsession.close();
    }

    @Override
    public HashMap getOrderConfig() {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketSystemMapper mapper = sqlsession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        String[] str = {"market_order_unconfirm", "market_order_unpaid", "market_order_comment"};
        criteria.andKeyNameIn(Arrays.asList(str));
        List<MarketSystem> marketSystems = mapper.selectByExample(marketSystemExample);
        HashMap<String, String> hashMap = new HashMap<>();

        for (MarketSystem marketSystem : marketSystems) {
            if (marketSystem != null) {
                String keyName = marketSystem.getKeyName();
                String keyValue = marketSystem.getKeyValue();
                hashMap.put(keyName, keyValue);
            }
        }

        sqlsession.commit();
        sqlsession.close();
        return hashMap;
    }

    @Override
    public void postOrderConfig(HashMap<String, String> hashMap) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketSystemMapper mapper = sqlsession.getMapper(MarketSystemMapper.class);

        String keyName1 = "market_order_unconfirm";
        String keyValue1 = hashMap.get(keyName1);
        String keyName2 = "market_order_unpaid";
        String keyValue2 = hashMap.get(keyName2);
        String keyName3 = "market_order_comment";
        String keyValue3 = hashMap.get(keyName3);

        mapper.updateOrderConfig(keyName1, keyValue1);
        mapper.updateOrderConfig(keyName2, keyValue2);
        mapper.updateOrderConfig(keyName3, keyValue3);

        sqlsession.commit();
        sqlsession.close();
    }
}
